<?php
session_start();
include_once ('class_master/db.class.php');
include_once ('class_master/parcourir.class.php');    
include_once ('includes/fonction.php');	

function VerifierIP($adresse)
{
	$Syntaxe='#^(?P<oct1>[0-9]{1,3})\.(?P<oct2>[0-9]{1,3})\.(?P<oct3>[0-9]{1,3})\.(?P<oct4>[0-9]{1,3})$#';
	if(preg_match($Syntaxe,$adresse))
		return true;
	else
		return false;
}


    if (isset($_POST['hidSalle'])&& $_POST['hidSalle'] == '1'){
        if (!is_numeric($_POST['nomSalle'])){
            $nomSalle = $_POST['nomSalle'];
            $id_salle = $_POST['id_salle'];
            $statutSalle = $_POST['statutSalle'];
            $db = new db();
            $SqlValues = ("'','".$nomSalle."','0'");
            $db->update('salle', $SqlValues);
        }else{
        	echo "erreur";
            //erreur
        }
    }else if (isset($_POST['hidMachine'])&& $_POST['hidMachine'] == '2'){
    	$nom_machine = $_POST['nomMachine'];
    	$new_nom_machine = $_POST['new_nomMachine'];
    	$ip_machine = $_POST['ipMachine'];
    	$statutMachine = $_POST['statutMachine'];
    	$selectSalle = $_POST['selectSalle'];
    	
    	if(!empty($nom_machine)){
    		if(!empty($ip_machine)){ 
    			   if(VerifierIP($ip_machine))
		            {
		            $db = new db();
		            $SqlValues = "'','$ip_machine','$new_nom_machine','0',$selectSalle";
		            $db->update('machine', $SqlValues);
		            }else{
		            	echo "erreur";
		            }
			}else {echo "erreur champ vide : IP de la machine";}
		}else {echo "erreur champ vide : nom de la machine";}
    }else if (isset($_POST['hidIncident'])&& $_POST['hidIncident'] == '3'){
    	$nomIncident = $_POST['nomIncident'];
    	$new_nom_incident = $_POST['new_nomIncident'];
   		$commentaireIncident = $_POST['commentaireIncident'];
   		$statutIncident = $_POST['statutIncident'];
   		
    	$id_machine = $_POST['selectMachine'];
    	 
    	if(!empty($nomIncident)){
    		if(!empty($id_machine)){
    				$db = new db();
    				$return = new parcourir();
    				
    				$SqlValues = "'";
    				 
    				$SqlValues = "'',now(),'','$nomIncident','1','$commentaireIncident','$id_machine'";
		            $db->update('incident', $SqlValues);
    				
		            
		            $SqlValues = "id_machine = $id_machine";
		            $req = $db->select('id_salle','machine', $SqlValues);
		            $selectSalle = $return->get_unique($req);
		            
		            
		            $SqlValues = $id_machine;
		            $db->update_status_machine('machine', $SqlValues);
		            
    				$SqlValues = $selectSalle;
    				$db->update_status_salle('salle', $SqlValues);
    				
    				
    				
    				
    
    		}else {echo "erreur champ vide : choisissez une machine";}
    	}else {echo "erreur champ vide : nom de l'incident";}
    }
            	//erreur
            //requete
            
if(empty($_GET['id_salle']) && isset($_POST['hidSalle']) && $_POST['hidSalle'] == '1')
{
	header("Location: gestion_salle.php");
}
elseif(empty($_GET['id_salle']) && isset($_POST['hidMachine']) && $_POST['hidMachine'] == '2')
{
	$selectSalle = $_POST['selectSalle'];
	header("Location: modif.php?id_salle=$selectSalle");
}elseif(empty($_GET['id_salle']) && isset($_POST['hidIncident']) && $_POST['hidIncident'] == '3')
{
	$id_machine = $_POST['selectMachine'];
	header("Location: modif.php?id_machine=$id_machine");
}

include('includes/header.php');
$req = new db();


?>
    
    <div id="contain">
        <?php include("includes/monitorEntete.php"); ?>
        <div id="corp">
            
            <div class="change" <?php echo $hidSalle;?>>
            <div class="title">Modification de Salle</div>
                <form>
                     <input type="hidden" name="hidSalle" id="hidSalle" value="1" />
                     <table>
                       <tr>
                       <td><label> Choix de la Salle :</label></td>
                         <td><select name="selectSalle" id="selectSalle">
                         
                        <?php
                        if (!empty($id_salle))
                        {
                        	echo '<option value="'.$id_salle.'">Salle actuelle</option>';
                        } 
                        $result =  $req->select_salle('id_salle, nom_salle');
                        while ($row = $result->fetch()) 
                        {
                        	$id_s = $row[0];
                        	$nom_s = $row[1];
                        	echo  '<option value="'.$id_s.'">'.$nom_s.'</option>';
                        }
                        ?>
                    	</select><br /></td>
                        </tr>
                        <tr>
                            <td><label> Nouveau nom de la Salle :</label></td>
                            <td><input type="text" name="new_nomSalle"   value="" /></td>
                        </tr>
                        <tr>
                            <td><label> Satut ok :</label></td>
                            <td><input type="radio" name="statutSalle"  value="0" /></td>
                        </tr>
                        <tr>  
                            <td><label> Satut ko :</label></td>
                            <td><input type="radio" name="statutSalle"  value="1" /></td>
                        </tr>
                     </table>
                     <button class="btn btn-small" type="submit">Valider</button>
                </form>
            </div>
            
            <div class="change deuxiemepart" <?php echo $hidMachine;?>>
            <div class="title">Modification de Machine</div>
               <form>
                    <input type="hidden" name="hidMachine" id="hidMachine" value="2" />
                    <table>
                        <?php 
                        
                        $result =  $req->select_all('*','machine');
                        
                        echo '
						<tr>
                       		<td><label> Choix de la Salle :</label></td>
                         	<td><select name="nomIncident" id="nomIncident">';
                             while ($row = $result->fetch()) 
		                     {
		                       	$id_m = $row[0];
		                       	$nom_m = $row[2];
		                       	echo  '<option value="'.$id_m.'">'.$nom_m.'</option>';
		                     }
		                     echo '
            					</select><br />
            					</td>';
		                     ?>
			            </tr>
			             <tr>
                            <td><label> Nouveau nom machine :</label></td>
	                        <td><input type="text" name="new_nomMachine" id="new_nomMachine" value="" /></td>
                        </tr>
            			<tr>
                            <td><label> Nouvelle IP :</label></td>
                            <td><input type="text" name="ipMachine" id="ipMachine" value="" /></td>
                        </tr>
                         <tr>
                            <td><label> Satut OK :</label><input type="radio" name="statutIncident"  value="0" /></td>
                         
                            <td><label> Satut KO :</label><input type="radio" name="statutIncident"  value="1" /></td>
                        </tr>
                        <tr>
                       		<td><label> Choix de la Salle :</label></td>
                             <td>
	                         <select name="selectSalle" id="selectSalle">
	                         
	                        <?php
	                        if (!empty($id_salle))
	                        {
	                        	echo '<option value="'.$id_salle.'">Salle actuelle</option>';
	                        } 
	                        $result =  $req->select_salle('id_salle, nom_salle');
	                        while ($row = $result->fetch()) 
	                        {
	                        	$id_s = $row[0];
	                        	$nom_s = $row[1];
	                        	echo  '<option value="'.$id_s.'">'.$nom_s.'</option>';
	                        }
	                        
	                        ?>
                    		</select><br />
                            </td>
                        </tr>
                     </table>
                    <button class="btn btn-small" type="submit">Valider</button>
                </form> 
            </div>
            
            
            <div class="change deuxiemepart" <?php echo $hidIncident;?>>
            <div class="title">Modification de Incident</div>
               <form>
                    <input type="hidden" name="hidIncident" id="hidIncident" value="3" />
                    <table>
                        <?php 
                        
                        $result =  $req->select_all('*','incident');
                        
                        echo '
						<tr>
                       		<td><label> Choix de l\'incident :</label></td>
                         	<td><select name="idMachine" id="idMachine">';
                             while ($row = $result->fetch()) 
		                     {
		                       	$id_m = $row[0];
		                       	$result =  $req->select('nom_machine','machine','id_machine ='.$row[6].' ');
		                       	$return = new parcourir();
		                       	$res = $return->get_unique($result);
		                       	echo $res;
		                       	$nom_m = $row[1]." : ".$row[3]." : ".$row[5]." : ".$res;
		                       	echo  '<option value="'.$id_m.'">'.$nom_m.'</option>';
		                     }
		                     echo '
            					</select><br />
            					</td>';
		                     ?>
			            </tr>
			             <tr>
                            <td><label> Nouveau nom incident :</label></td>
	                        <td><input type="text" name="new_nomIncident" id="new_nomIncident" value="" /></td>
                        </tr>
            			<tr>
                            <td><label> Nouveau commentaire incident :</label></td>
                            <td><input type="text" name="commentaireIncident" id="commentaireIncident" value="" /></td>
                        </tr>
                         <tr>
                            <td><label> Satut OK :</label><input type="radio" name="statutMachine"  value="0" /></td>
                         
                            <td><label> Satut KO :</label><input type="radio" name="statutMachine"  value="1" /></td>
                 		</tr>
			            	<tr>
			             	<td><input type="button" name="finIncident" id="finIncident" value="Cloturer de force" /></td>
                   			</tr>
			             <tr>
                     </table>
                    <button class="btn btn-small" type="submit">Valider</button>
                </form> 
            </div>
            
            
        </div>
        
        <div id="main">
            <?php include('includes/main.php'); ?>
        </div>
        <?php include('includes/etatEnDirect.php'); ?>
        
    </div>
<?php include('includes/footer.php'); ?>